package com.bite.bite_forum.controller;
import com.bite.bite_forum.common.AppConfig;
import com.bite.bite_forum.common.AppResult;
import com.bite.bite_forum.common.ResultCode;
import com.bite.bite_forum.exception.ApplicationException;
import com.bite.bite_forum.model.User;
import com.bite.bite_forum.services.IUserService;
import com.bite.bite_forum.utils.MD5Utils;
import com.bite.bite_forum.utils.StringUtils;
import com.bite.bite_forum.utils.UUIDUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.NonNull;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

@Slf4j
@RestController
@Api(tags = "用户接口")
@RequestMapping("/user")
public class UserController {
    @Resource
    private IUserService userService;

    @ApiOperation("注册接口")
    @PostMapping("/register")
    public AppResult register(@ApiParam("用户名") @RequestParam("username") @NonNull String username,
                              @ApiParam("昵称") @RequestParam("nickname") @NonNull String nickname,
                              @ApiParam("密码") @RequestParam("password") @NonNull String password,
                              @ApiParam("确认密码") @RequestParam("passwordRepeat") @NonNull String passwordRepeat){
        //
        User user = new User();
        user.setUsername(username);
        user.setNickname(nickname);
        if(!password.equals(passwordRepeat)){
            log.info(ResultCode.FAILED_TWO_PWD_NOT_SAME.getMessage());
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_TWO_PWD_NOT_SAME));
        }
        String salt = UUIDUtils.UUID_32();
        String ciphertext = MD5Utils.md5salt(password, salt);
        user.setSalt(salt);
        user.setPassword(ciphertext);
        userService.createNormalUser(user);
        return AppResult.success();

    }

    @ApiOperation("用户登录")
    @PostMapping("/login")
    public AppResult login(@ApiParam("用户名") @RequestParam("username") String username,
                      @ApiParam("密码") @RequestParam("password")String password, HttpSession session){
        User user = userService.login(username, password);
        if(user == null){
            log.info(ResultCode.FAILED_USER_NOT_EXISTS.getMessage());
        }
        session.setAttribute(AppConfig.USER_SESSION, user);
        return AppResult.success();
    }

    @ApiOperation("获取用户信息")
    @PostMapping("/info")
    public AppResult<User> info(HttpServletRequest request,
                                @ApiParam("用户id")@RequestParam(value = "id", required = false) Long id){
        User user = userService.selectById(request, id);
        return AppResult.success(user);
    }

    @ApiOperation("用户退出")
    @PostMapping("/logout")
    public AppResult logout(HttpServletRequest request){
        HttpSession session = request.getSession(false);
        if(session != null){
            log.info("退出成功");
            session.invalidate();
        }
        return AppResult.success();
    }


    @ApiOperation("修改个人信息")
    @PostMapping("/modifyInfo")
    public AppResult modifyInfo(HttpServletRequest request,
                                @ApiParam("用户名") @RequestParam(value = "username", required = false) String username,
                                @ApiParam("昵称") @RequestParam(value = "nickname", required = false) String nickname,
                                @ApiParam("性别") @RequestParam(value = "gender", required = false) Byte gender,
                                @ApiParam("邮箱") @RequestParam(value = "email", required = false) String email,
                                @ApiParam("电话") @RequestParam(value = "phoneNum", required = false) String phoneNum,
                                @ApiParam("个人简介") @RequestParam(value = "remark", required = false) String remark){
        //非空校验
        if(StringUtils.isEmpty(username) && StringUtils.isEmpty(nickname) && StringUtils.isEmpty(email)
                && StringUtils.isEmpty(phoneNum) && gender == null
                && StringUtils.isEmpty(remark)){
            return AppResult.failed("请输入要修改的内容");
        }
        //获取用户id
        HttpSession session = request.getSession();
        User user = (User) session.getAttribute(AppConfig.USER_SESSION);
        User updateUser = new User();
        updateUser.setId(user.getId());
        //封装对象
        updateUser.setUsername(username);
        updateUser.setUsername(nickname);
        updateUser.setGender(gender);
        updateUser.setEmail(email);
        updateUser.setPhoneNum(phoneNum);
        updateUser.setRemark(remark);
        //调用Service
        userService.modifyInfo(updateUser);
        //将最新的用户信息存到session中
        user = userService.selectByPrimaryKey(user.getId());
        session.setAttribute(AppConfig.USER_SESSION, user);
        return AppResult.success();

    }

    @ApiOperation("修改密码")
    @PostMapping("/modifyPwd")
    public AppResult modifyPassword(HttpServletRequest request,
                                   @ApiParam("原密码") @RequestParam("oldPassword") @NonNull String oldPassword,
                                    @ApiParam("新密码") @RequestParam("newPassword") @NonNull String newPassword,
                                    @ApiParam("确认密码") @RequestParam("passwordRepeat") @NonNull String passwordRepeat){
        //验证两次密码
        if(!newPassword.equals(passwordRepeat)){
            log.info(ResultCode.FAILED_TWO_PWD_NOT_SAME.getMessage());
            return AppResult.failed(ResultCode.FAILED_TWO_PWD_NOT_SAME);
        }
        //获取用户id
        HttpSession session = request.getSession();
        User user = (User) session.getAttribute(AppConfig.USER_SESSION);
        userService.modifyPassword(user.getId(), oldPassword, newPassword);
        return AppResult.success();
    }

}
